package org.dydl.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.IGoodsRepository;
import org.dydl.dao.system.repository.ILossRepository;
import org.dydl.dao.system.repository.IOrderDetailRepository;
import org.dydl.dao.system.repository.IOrderRepository;
import org.dydl.dao.system.repository.IProduceRepository;
import org.dydl.dao.system.repository.IProductionDetailRepository;
import org.dydl.dao.system.repository.LossDetailRepository;
import org.dydl.model.system.Lossdetail;
import org.dydl.model.system.Production;
import org.dydl.model.system.Productiondetail;
import org.dydl.model.system.vo.ProductionDetailVO;
import org.dydl.model.system.vo.RegisterVO;
import org.dydl.model.system.vo.RequiredMaterialsVO;
import org.dydl.service.system.ProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ProduceServiceImpl implements ProduceService{

	@Autowired
	private IProduceRepository produceRepository;
	@Autowired
	private IProductionDetailRepository iProductionDetailRepository;
	@Autowired
	private ILossRepository iLossRepository;
	@Autowired
	private LossDetailRepository lossDetailRepository;
	@Autowired
	private IOrderDetailRepository iOrderDetailRepository;
	@Autowired
	private IOrderRepository iOrderRepository;
	@Autowired
	private IGoodsRepository iGoodsRepository;
	
	
	private ResponseMessage res = new ResponseMessage();
	
	/**
	 * 根据订单号查询生产任务单表头
	 * @param orderNo
	 * @return
	 */
	@Override
	public ResponseMessage list(String orderNo) {
		Production production = null;
		try {
			production = produceRepository.list(orderNo);
			res.setData(production);
			res.setSuccess(true);
		} catch (Exception e) {
			res.setMsg(e.getMessage());
			res.setSuccess(false);
		}
		return res;
	}

	/**
	 * 添加生产任务单 表头表体一起保存
	 * @param detailVO
	 * @return
	 */
	@Override
	@Transactional
	public ResponseMessage bothSave(ProductionDetailVO detailVO) {
		try {
			produceRepository.save(detailVO.getProduction());
			iProductionDetailRepository.save(detailVO.getArr());
			iOrderRepository.changeProductionInstructionStatus(detailVO.getProduction().getOrderNo());
			res.setSuccess(true);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 查询生产任务列表
	 * @param page
	 * @param rows
	 * @param productionNo
	 * @return
	 */
	@Override
	public ResponseMessage getList(int page, int rows, String productionNo) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Production> pageList = null;
		if (StringUtils.isNotBlank(productionNo)) {
			pageList = produceRepository.getProduceListLike(String.format("%s%s%s", "%", StringUtils.trim(productionNo), "%"),
					pageable);
		} else {
			pageList = produceRepository.getProduceList(pageable);
		}
		return new ResponseMessage(true, new ResponseData<Production>(page, pageList.getTotalPages(),
				((int) pageList.getTotalElements()), pageList.getContent()));
	}

	/**
	 * 查询生产任务明细
	 * @param page
	 * @param rows
	 * @param orderNo
	 * @return
	 */
	@Override
	public ResponseMessage getDetailList(int page, int rows, String orderNo) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Productiondetail> pageList = null;
		if (StringUtils.isNotBlank(orderNo)) {
			pageList = iProductionDetailRepository.getProduceListLike(String.format("%s%s%s", "%", StringUtils.trim(orderNo), "%"),
					pageable);
		} else {
			pageList = iProductionDetailRepository.getProduceList(pageable);
		}
		return new ResponseMessage(true, new ResponseData<Productiondetail>(page, pageList.getTotalPages(),
				((int) pageList.getTotalElements()), pageList.getContent()));
	}

	/**
	 * 查询生产任务成品所需物料列表
	 * @param orderNo
	 * @return
	 */
	@Override
	public ResponseMessage getLoss(String orderNo) {
		try {
			List<RequiredMaterialsVO> rmList = new ArrayList<>();
			List<Productiondetail> list = iProductionDetailRepository.getDetail(orderNo);
			String goodsUuid = null;
			List<Lossdetail> lossDetailList = null;
			
			String bomNo;
			
				for (Productiondetail productiondetail : list) {
					goodsUuid = productiondetail.getGoodsUuid();
					bomNo = iLossRepository.findByGoodsUuid(goodsUuid);
					if(bomNo == null){
						break;
					}
					lossDetailList = lossDetailRepository.findByBomNo(bomNo);
				}
				for (Lossdetail lossdetail : lossDetailList) {
					RequiredMaterialsVO requiredMaterialsVO = new RequiredMaterialsVO();
					requiredMaterialsVO.setName(lossDetailRepository.findNameByUuid(lossdetail.getGoodsUuid())); 
					requiredMaterialsVO.setModel(lossDetailRepository.findModelByUuid(lossdetail.getGoodsUuid())); 
					requiredMaterialsVO.setInStockQty(iGoodsRepository.findInStockQTYByUUID(lossdetail.getGoodsUuid()));
					requiredMaterialsVO.setDecCm(lossDetailRepository.findDeccmByUuid(lossdetail.getGoodsUuid()));
					rmList.add(requiredMaterialsVO);
				}
				res.setData(rmList);
				res.setSuccess(true);
		} catch (Exception e) {
				res.setMsg(e.getMessage());
				res.setSuccess(false);
		}
		return res;
	}

	/**
	 * 登记入库
	 * @param registerVO
	 * @return
	 */
	@Override
	@Transactional
	public ResponseMessage doRegister(RegisterVO registerVO) {
		try {
			iOrderDetailRepository.updateAlreadyByOrderDetailUuid(registerVO.getOrderDetailUuid(),registerVO.getAlready());
			
			iGoodsRepository.updateGoodsStockQtyByGoodsUUID(registerVO.getAlready(),registerVO.getGoodsUuid());
			
			iProductionDetailRepository.updateRegister(registerVO.getAlready(),registerVO.getRegister(),registerVO.getRegisterDate(),registerVO.getOrderDetailUuid());
			
			produceRepository.updateAlready(registerVO.getAlready(),registerVO.getOrderNo());
			
			res.setSuccess(true);
		} catch (Exception e) {
			res.setMsg(e.getMessage());
			res.setSuccess(false);
		}
		return res;
	}

	/**
	 * 审核
	 * @param id
	 */
	@Override
	public ResponseMessage doExamination(Integer id,Production production) {
		
		int amount;
		String bomNo;
		int deccm;
		int inStockQTY;
		List<Lossdetail> lossDetailList = null;
		List<Productiondetail> productiondetails = iProductionDetailRepository.getDetail(production.getOrderNo());
		
		try {
			for (Productiondetail productiondetail : productiondetails) {
			amount = productiondetail.getAmount();//订单成品总数
			bomNo  =  iLossRepository.findByGoodsUuid(productiondetail.getGoodsUuid());
			if(bomNo == null){
				break;
			}
			lossDetailList = lossDetailRepository.findByBomNo(bomNo);
				for (Lossdetail lossdetail : lossDetailList) {
					deccm = (lossdetail.getDecCm()).intValue();
					inStockQTY = iGoodsRepository.findInStockQTYByUUID(productiondetail.getGoodsUuid()); //库存
					if (inStockQTY - (amount * deccm)<0) {
						res.setStatus(1);
						res.setSuccess(false);
						res.setMsg("物料库存不足，不能生产！");
						return res;
					}
				}
			}
			produceRepository.doExamination(id);
			res.setSuccess(true);
		} catch (Exception e) {
			res.setMsg(e.getMessage());
			res.setSuccess(false);
		}
		
		return null;
	}
	/**
	 * 保存生产任务单表头
	 * @param production
	 * @return
	 */
	@Override
	public ResponseMessage save(Production production) {
		try {
			produceRepository.save(production);
			res.setSuccess(true);
		} catch (Exception e) {
			res.setMsg(e.getMessage());
			res.setSuccess(false);
		}
		return res;
	}

	/**
	 * 完成订单
	 */
	@Override
	public ResponseMessage complete(Integer id) {

		try {
			produceRepository.complete(id);
			res.setSuccess(true);
		} catch (Exception e) {
			res.setSuccess(false);
			res.setMsg(e.getMessage());
		}
		return res;
	}

	@Override
	public ResponseMessage getStowageList() {
		
		try {
			res.setSuccess(true);
			res.setData(iProductionDetailRepository.getStowageList());
		} catch (Exception e) {
			res.setMsg(e.getMessage());
			res.setSuccess(false);
		}
		return res;
	}

}
